package com.ydd.util.glide;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget;
import com.taofengsdk.R;

/**
 * Created by tao on 17-3-12.
 *
 * @function 初始化Glide（单例模式），并加载网络图片
 */

public class GlideManager {
    private Context mContext;

    //Glide配置的参数
    private static final int THRED_COUNT = 4;
    private static final int PRIORIY = 2;
    private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;
    private static final int CONNECTION_TIME_OUT = 5 * 1000;
    private static final int READ_TIME_OUT = 30 * 1000;

//    private static GlideManager mGlideManager = null;
//
//    public static GlideManager getInstance(Context context) {
//        if (mGlideManager != null) {
//            mGlideManager = new GlideManager(context);
//        }
//
//        return mGlideManager;
//    }

    /**
     * 单例模式私有构造方法
     *
     * @param
     */
//    private GlideManager(Context context) {
//        this.mContext = context;
//    }

    //默认加载
//    public void loadImageView(String path, ImageView mImageView) {
//        Glide.with(mContext).load(path).into(mImageView);
//    }

    public static void loadImageView(Context context,String path, ImageView mImageView) {
        Glide.with(context).load(path).placeholder(R.drawable.xadsdk_img_error)
                .error(R.drawable.xadsdk_img_error).priority(Priority.NORMAL)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mImageView);
    }

    public void loadImageView(String path, ImageView mImageView
            , RequestListener<String, GlideDrawable> requstlistener) {
        Glide.with(mContext).load(path).placeholder(R.drawable.xadsdk_img_error)
                .error(R.drawable.xadsdk_img_error).priority(Priority.NORMAL)
                .diskCacheStrategy(DiskCacheStrategy.ALL).listener(requstlistener)
                .into(mImageView);
    }

    //加载指定大小
    public void loadImageViewSize(String path, int width, int height
            , ImageView mImageView) {
        Glide.with(mContext).load(path).override(width, height).into(mImageView);
    }

    //设置加载中以及加载失败图片
    public void loadImageViewLoding(String path, ImageView mImageView
            , int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }

    //设置加载中以及加载失败图片并且指定大小
    public void loadImageViewLodingSize(String path, int width, int height
            , ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(path).override(width, height).placeholder(lodingImage)
                .error(errorImageView).into(mImageView);
    }

    //设置跳过内存缓存
    public void loadImageViewCache(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).skipMemoryCache(true).into(mImageView);
    }

    //设置下载优先级
    public void loadImageViewPriority(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).priority(Priority.NORMAL).into(mImageView);
    }

    /**
     * 策略解说：
     * <p>
     * all:缓存源资源和转换后的资源
     * <p>
     * none:不作任何磁盘缓存
     * <p>
     * source:缓存源资源
     * <p>
     * result：缓存转换后的资源
     */

    //设置缓存策略
    public void loadImageViewDiskCache(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).diskCacheStrategy(DiskCacheStrategy.ALL).into(mImageView);
    }

    /**
     * api也提供了几个常用的动画：比如crossFade()
     */

    //设置加载动画
    public void loadImageViewAnim(String path, int anim, ImageView mImageView) {
        Glide.with(mContext).load(path).animate(anim).into(mImageView);
    }

    /**
     * 会先加载缩略图
     */

    //设置缩略图支持
    public void loadImageViewThumbnail(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).thumbnail(0.1f).into(mImageView);
    }

    /**
     * api提供了比如：centerCrop()、fitCenter()等
     */

    //设置动态转换
    public void loadImageViewCrop(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).centerCrop().into(mImageView);
    }

    //设置动态GIF加载方式
    public void loadImageViewDynamicGif(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).asGif().into(mImageView);
    }

    //设置静态GIF加载方式
    public void loadImageViewStaticGif(String path, ImageView mImageView) {
        Glide.with(mContext).load(path).asBitmap().into(mImageView);
    }

    //设置监听的用处 可以用于监控请求发生错误来源，以及图片来源 是内存还是磁盘

    //设置监听请求接口
    public void loadImageViewListener(String path, ImageView mImageView
            , RequestListener<String, GlideDrawable> requstlistener) {
        Glide.with(mContext).load(path).listener(requstlistener).into(mImageView);
    }

    //项目中有很多需要先下载图片然后再做一些合成的功能，比如项目中出现的图文混排

    //设置要加载的内容
    public void loadImageViewContent(String path, SimpleTarget<GlideDrawable> simpleTarget) {
        Glide.with(mContext).load(path).centerCrop().into(simpleTarget);
    }

    //清理磁盘缓存
    public void GuideClearDiskCache() {
        //理磁盘缓存 需要在子线程中执行
        Glide.get(mContext).clearDiskCache();
    }

    //清理内存缓存
    public void GuideClearMemory() {
        //清理内存缓存  可以在UI主线程中进行
        Glide.get(mContext).clearMemory();
    }
}
